﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.DomainLayer;

using HAUtil;

using DotSpatial.Topology;


using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization;





namespace HA.Data
{

    /// <summary>
    /// 具体的feature实体
    /// </summary>
    [Serializable]
   public abstract class FeatureObject:ICloneable
    {

        public FeatureObject(string id,Coverage parent)
        {
            this._parent = parent;

            this._layerRange = new LayerRange();

            this._id = id;

            this._name = id;



            this._selectedSCType = MAPSCType.NONE;


            //应力期数据

            int nPeriods = this._parent.ParentCM.Model.NPeriods;

            //取得污染物设置


            List<string> lstNames = new List<string>();

            foreach (MT3DMS.DomainLayer.MTSpecies item in this._parent.ParentCM.Model.SpeList)
            {

                lstNames.Add(item.Name);

            }


            this._specifiedHead = new BCSpecifiedHead(lstNames);

            this._river = new BCRiver();

            this._drain = new BCDrain();

            this._seepage = new BCSeepageFace();


            this._generalHead = new BCGeneralHead();


          


            
            
            ///纯mt3dms
            ///




            this._specConc = new BCSpecConc(lstNames);

            this._massLoading = new BCMassLoading(lstNames);




 
        }

        #region New

        Coverage _parent;
        /// <summary>
        /// 父容器
        /// </summary>
        public Coverage Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        #endregion


        string _id;

        /// <summary>
        /// 唯一标识符
        /// </summary>
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
        


        string _name;
        /// <summary>
        /// 对外使用的名称描述
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        //protected BCType _type;

        //public BCType Type
        //{
        //    get { return _type; }
        //    set { _type = value; }
        //}




        protected MAPSCType _selectedSCType;

        public MAPSCType SelectedSCType
        {
            get { return _selectedSCType; }
            set { _selectedSCType = value; }
        }



        //BCBarrier _barrier;

        //public BCBarrier Barrier
        //{
        //    get { return _barrier; }
        //    set { _barrier = value; }
        //}


        protected LayerRange _layerRange;

        public LayerRange LayerRange
        {
            get { return _layerRange; }
          
        }

        protected BCSpecifiedHead _specifiedHead;

        public BCSpecifiedHead SpecifiedHead
        {
            get { return _specifiedHead; }
            set { _specifiedHead = value; }
        }


        protected BCRiver _river;

        public BCRiver River
        {
            get { return _river; }
            set { _river = value; }
        }

        protected BCDrain _drain;

        public BCDrain Drain
        {
            get { return _drain; }
            set { _drain = value; }
        }


        protected BCSeepageFace _seepage;

        public BCSeepageFace Seepage
        {
            get { return _seepage; }
            set { _seepage = value; }
        }


        protected BCGeneralHead _generalHead;

        public BCGeneralHead GeneralHead
        {
            get { return _generalHead; }
            set { _generalHead = value; }
        }



       //mt3dms部分的属性

        protected BCSpecConc _specConc;

        public BCSpecConc SpecConc
        {
            get { return _specConc; }
            set { _specConc = value; }
        }

        protected BCMassLoading _massLoading;

        public BCMassLoading MassLoading
        {
            get { return _massLoading; }
            set { _massLoading = value; }
        }
     

        //List<BCMT3DMS> _mtProps;

        //public List<BCMT3DMS> MtProps
        //{
        //    get { return _mtProps; }
            
        //}




        #region 受保护的方法

      


    


        //用于暂时存储对应的结果
        [NonSerialized]
        protected List<ValueResult> _lstIJK=new List<ValueResult> ();

        public List<ValueResult> LstIJK
        {
            get { return _lstIJK; }
            set { _lstIJK = value; }
        }


        /// <summary>
        /// 构建对应的IJK列表
        /// </summary>
        /// <param name="polys"></param>
        /// <param name="ibound"></param>
        public  void BuildIJKList(Polygon[,] polys, List<int[,]> ibound,Polygon polyGridFrame,bool isUseDefaultLayerRange,int defaultFrom,int defaultTo)
        {

            if (isUseDefaultLayerRange)
            {
                this.BuildIJKList(polys, ibound, polyGridFrame, defaultFrom, defaultTo);


            }

            else
            {
                this.BuildIJKList(polys, ibound, polyGridFrame, this.LayerRange.FromLayer, this.LayerRange.ToLayer);
            }
            

 
        }


        /// <summary>
        /// 这个函数要求执行具体的操作
        /// </summary>
        /// <param name="polys"></param>
        /// <param name="ibound"></param>
        /// <param name="From"></param>
        /// <param name="To"></param>
        protected virtual void BuildIJKList(Polygon[,] polys, List<int[,]> ibound, Polygon polyGridFrame, int From, int To)
        {




 
        }


        protected internal virtual BCType GetCurrentBCType()
        {

            BCType bc = null;


            switch (this._selectedSCType)
            {
               
                //case MAPSCType.WELL:
                //    break;
             
                case MAPSCType.SPECIFIEDHEAD:


                    bc=this._specifiedHead;

                    break;

                //case MAPSCType.SPECIFIEDFLOW:

                //    bc = this._specifiedFlow;

                //    break;


                case MAPSCType.GENERALHEAD:

                    bc = this._generalHead;
                    break;
                case MAPSCType.DRAIN:
                    bc = this._drain;

                    break;
                case MAPSCType.RIVER:

                    bc = this._river;

                    break;
                //case MAPSCType.LAKE:
                //    break;
                //case MAPSCType.BARRIER:
                //    break;
                //case MAPSCType.OBSERVEDFLOW:
                //    break;
                //case MAPSCType.TANSOBSERVEDFLOW:

                //    break;

                case MAPSCType.SEEPAGEFACE:

                    bc = this._seepage;
                    break;
                case MAPSCType.NONE:
                    break;
                default:
                    break;
            }

            return bc;
 
        }

        protected BCMT3DMS GetCurrentMTType()
        {

               BCMT3DMS bc = null;


               switch (this._selectedSCType)
               {
                   case MAPSCType.SPECIFIEDCONC:

                       bc = this._specConc;
                       break;

                   case MAPSCType.MASSLOADING:

                       bc = this._massLoading;
                       break;
                   default:
                       break;


               }

               return bc;
 
        }


        #endregion


        public virtual void MapToModFlow(List<double> stressPeriods, ModFlowData mf, CoverageSetup setup)
        {

            BCType bc = this.GetCurrentBCType();
            if (bc == null)
            {
                return;

            }
            else
            {


                ///采用新的ijk算法
                ///



                //List<ValueResult > ijkList = this.GetIJKList(mf.GridData ,setup );


                List<ValueResult> ijkList = this.LstIJK;
             



                bc.MapToModflow(stressPeriods, ijkList,mf,this);


            }



           
 
        }



        /// <summary>
        /// 映射mt3dms部分
        /// </summary>
        /// <param name="data"></param>
        /// <param name="setup"></param>
        public virtual void MapToMT3dMS(List<double> stressPeriods, HA.MT3DMS.DomainLayer.MT3DMSData data ,CoverageSetup setup)
        {
            BCMT3DMS bcmt = this.GetCurrentMTType();

            if (bcmt == null)
            {


            }

            else
            {
                bcmt.MapToMT3DMs(stressPeriods,data, setup, this);
             
            }
         
 
        }




        public virtual void ResetCoords(List<HCoordinate> coords)
        {

 
        }

        /// <summary>
        /// 
        /// 取得当前图形的量纲，线指的是总长度，面指的是总面积
        /// </summary>
        /// <returns></returns>
        public virtual double GetTotalLValue()
        {
            return 0;
 
        }



        public virtual object Clone()
        {
            FeatureObject fo = (FeatureObject)this.MemberwiseClone();
            fo._river =(BCRiver) this._river.Clone();
            fo._layerRange = (LayerRange)this._layerRange.Clone();
            fo._specifiedHead = (BCSpecifiedHead)this._specifiedHead.Clone();

            fo._drain = (BCDrain)this._drain.Clone();

            fo._seepage = (BCSeepageFace)this._seepage.Clone();

            fo._generalHead = (BCGeneralHead)this._generalHead.Clone();


            fo._massLoading =(BCMassLoading) this._massLoading.Clone();
            fo._specConc = (BCSpecConc)this._specConc.Clone();



            //关于新改的映射规则部分

            fo._lstIJK = new List<ValueResult>();


            return fo;
          


          
        }



        internal virtual void OnSpeListChanged(List<string> lst)
        {



            this._specConc.OnSpeListChanged(lst);
            this._massLoading.OnSpeListChanged(lst);



            //foreach (MT3DMS.DomainLayer.MTSpecies item in lst)
            //{
            //    List<double> arry = new List<double>();
            //    this._specConc.ArrySpecConc.Add(arry);        
                
            //}

 
        }


        //internal virtual void OnNPeriodsChanged(int nPeriods)
        //{

        //    this._river.NPeriods = nPeriods;
         
        //    this._specifiedHead.NPeriods = nPeriods;

        //    this._drain.NPeriods = nPeriods;

        //    this._seepage.NPeriods = nPeriods;

        //    this._generalHead.NPeriods = nPeriods;


        //    //mt3dms部分

        //    this._massLoading.NPeriods = nPeriods;
        //    this._specConc.NPeriods = nPeriods;



 
        //}



        #region 反序体制

        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {
            this._lstIJK = new List<ValueResult>();

        }

        [OnSerializing]
        public void OnSerializing(StreamingContext context)
        {




        }

        #endregion

    }
}
